import java.io.*;
import java.util.Scanner;

public class DiaryApp {
    private static String currentFilePath;
    private static BufferedWriter writer;
    private static BufferedReader reader;
    private static boolean isNewFile = true;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("请选择操作：");
            System.out.println("1. 新建日记本");
            System.out.println("2. 打开日记本");
            System.out.println("3. 修改日记本");
            System.out.println("4. 保存");
            System.out.println("5. 退出");
            choice = scanner.nextInt();
            scanner.nextLine();  // Consume newline  

            switch (choice) {
                case 1:
                    newDiary();
                    break;
                case 2:
                    openDiary();
                    break;
                case 3:
                    editDiary(scanner);
                    break;
                case 4:
                    saveDiary();
                    break;
                case 5:
                    System.out.println("退出系统");
                    break;
                default:
                    System.out.println("无效选择，请重新选择");
            }
        } while (choice != 5);

        closeResources();
    }

    private static void newDiary() {
        currentFilePath = null;
        isNewFile = true;
        System.out.println("请输入日记内容（输入'END'结束）：");
        try (Scanner scanner = new Scanner(System.in)) {
            StringBuilder diaryContent = new StringBuilder();
            while (true) {
                String line = scanner.nextLine();
                if (line.equalsIgnoreCase("END")) {
                    break;
                }
                diaryContent.append(line).append(System.lineSeparator());
            }
            if (diaryContent.length() > 0) {
                System.out.println("日记内容已录入，准备保存。请输入保存路径：");
                currentFilePath = scanner.nextLine();
                try (BufferedWriter writer = new BufferedWriter(new FileWriter(currentFilePath))) {
                    writer.write(diaryContent.toString());
                } catch (IOException e) {
                    System.err.println("保存日记本时出错: " + e.getMessage());
                }
            } else {
                System.out.println("未输入任何内容，跳过保存。");
            }
        }
    }

    private static void openDiary() {
        System.out.println("请输入日记本文件路径：");
        Scanner scanner = new Scanner(System.in);
        currentFilePath = scanner.nextLine();
        isNewFile = false;
        try (BufferedReader reader = new BufferedReader(new FileReader(currentFilePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            System.err.println("文件未找到: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("读取日记本时出错: " + e.getMessage());
        }
    }

    private static void editDiary(Scanner scanner) {
        if (currentFilePath == null || isNewFile) {
            System.out.println("当前没有打开的日记本，请先进行新建或打开操作。");
            return;
        }
        System.out.println("请输入要添加的日记内容（输入'END'结束）：");
        StringBuilder additionalContent = new StringBuilder();
        while (true) {
            String line = scanner.nextLine();
            if (line.equalsIgnoreCase("END")) {
                break;
            }
            additionalContent.append(line).append(System.lineSeparator());
        }
        if (additionalContent.length() > 0) {
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(currentFilePath, true))) {
                writer.write(additionalContent.toString());
            } catch (IOException e) {
                System.err.println("修改日记本时出错: " + e.getMessage());
            }
        } else {
            System.out.println("未输入任何内容，跳过修改。");
        }
    }

    private static void saveDiary() {
        if (currentFilePath == null) {
            System.out.println("没有需要保存的日记本。");
            return;
        }
        // In this case, we don't need to explicitly save as new diary content  
        // is immediately written to file in newDiary() or editDiary().  
        System.out.println("日记本已保存到: " + currentFilePath);
    }

    private static void closeResources() {
        try {
            if (writer != null) {
                writer.close();
            }
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            System.err.println("关闭资源时出错: " + e.getMessage());
        }
    }
}